package com.bruce.tool.pay.alipay.app.back;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {
    public static void main(String[] args) throws Exception {
//        appPay();
        wapPay();
    }
    private static void appPay(){
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradeAppPayResponse response = Payment.App()
                    .optional("goods_type", "0")
                    .optional("timeout_express", "30m")
                    .optional("passback_params", "a%3D1%26b%3D2")
                    .optional("product_code", "QUICK_MSECURITY_PAY")
                    .pay("测试订单支付", "1234567890", "0.01");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println(response.body);
            } else {
                System.err.println("调用失败，原因：" + response.body);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    private static void wapPay(){
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradeWapPayResponse response = Payment.Wap()
                    .optional("goods_type", "0")
                    .optional("timeout_express", "30m")
                    .optional("passback_params", "a%3D1%26b%3D2")
                    .optional("product_code", "QUICK_MSECURITY_PAY")
                    .pay("测试支付宝-网页支付", "12809328138", "0.01", "http://www.baidu.com", "http://www.baidu.com");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println(response.body);
            } else {
                System.err.println("调用失败，原因：" + response.body);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipay.com";
        config.signType = "RSA2";

        // <-- 请填写您的AppId，例如：2019091767145019 -->
        config.appId = "2021001144608093";

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        // <-- 请填写您的应用私钥，例如：MIIEvQIBADANB ... ... -->
        config.merchantPrivateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCCYJiOBrRuqmwYNLmoisqaGYDOtrYU47ogCH0IMIx/AeOkWOMpsr7WT9CvcyiVCtlF9f2lGk7JdcucxxBm4swAMUmnFYlScy3APRt+GP8SEa1xcHz3KH6UwRU5wslMIMQEWdDGtIkraty1M2IAwzY1aqOrfp2MHDdRB2XGFFuqgJVe7Rc/+5/roEb7QAiQc+yYF1qCU6BqoGZVitDKWOf7uB3ZzeljK6uKYdOfYnrsPHMM03NyhiyqnoAKHJlYcfBJ/MBSG97wcOO8M+COf1EEuo62QNtG2nkcUWDRE88TCz4835VdG8mSh+eFWCKMIL310F73NS48DCJRJd+a+JfPAgMBAAECggEARJyDDBMZNzf2hNKSddD8iBYtxBtBPUMOuzbcXcf22AvteZrRiSso5O3B++MVk0HPT9MBtbZfigO0sH/N8OcfxmYpxYU+7CXzCLzfz/9llGO7K/yvtv4Xhz8JZYlNmAOSRFvj0/5ERZYKKk2tIIua5oeFNIYAcTCUJ89FzvT9vH8IUaYaI1XyZyXF58/UAsIhF7sUWMq4evG0wxe/Qal3UIAG+5U91QBbbBMxW91ntToBiwwk8ce61vQ9+8FLNrt6Zy8T6f9WIK+YeasOYT2tTZ4uPN9xtVDWEA0cp2NeMJS0A8a6sw+AroIrxms0wb9i/2/aaK6/tAQjLKJnKln5sQKBgQC4A5cjE0CX/RGbfy8joiqfU4dMRl162haNewmajTmCimLVzSVjQAFXhbhnlWIVIKyrUsU+li3j49uKpkUy18cFvldJFULE1WJCa+X1/BjF0iYavw7MBHU9E8Hgg6wb9srSioyljcuDMDlpRj3WqAUcePJ5fWn2VXYL4cD3ER5P1wKBgQC1YXa4EBQY7dYEG1fkrncdlfGFnR+jKnXxEz1fpY+9A/UvJFPV91r87MscOQpp+hL6M1B39kwAqusnF5YeTV84DEcE5+xv46B2PPEvR/zuHfxg8OGX/IlqyiJ8n9sUn0b0pBqjyd0k3uHUrc/Q8y6qFaIydaabYYOakzB559ZYyQKBgHRsERtn+t4uhw1SkoiA+WaEen2Pp2UqxkoNJi5aW6Bz3cRXXuhEfpTZTap7oOqtGZHrdE/8xFwCtcwrFmzsiEbh/meGxA+00c4kZgIT1biTFPinGL2LtIFIFsnthN7MbdZ3QjUcWT5PLIOmpUlCVLmG7CJhbsEjoOQzmNq4Qe1RAoGAXCUNeOjCLP/2r9NsTYLMhL0+G9D815mknTpW/ZZQdYfjMXXVGWg2UzPA46+euMQQjAcxpB/98IDkQdHbs70+2Gx0T+u61ATh6e9/37NErXuSUXVORjZHYXcbGhDj7ZAVLgeIeRj+PdL1C2TZpJ9GX6Xp5pSadWmOwCJyiBvdavECgYBryRfSmKZ5IdrlrKb81QlVJxC1lT5E/plFmfO9b/VsmD0LqHsNWnHs+0Kjo5D/y6qOrgPyUxRhqNYHurjHUp4nbUv+KnwNJG2STJCjv4+qnL9HhnhGdx0DWiZb6W+825TprUtu72/La/Zbk8qvClcbrhSSU45HJtFKolXRnGVaoQ==";

        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
//        config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->";
//        config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->";
//        config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->";

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        // <-- 请填写您的支付宝公钥，例如：MIIBIjANBg... -->
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0wInCen3Wxe7+HlOqKwJ0F79vn7b9O4MB2KXOCswvjZZpksJH+sZHX7F9jWWDs/4JseyNUZE5SB6krLmSN+DbGodKe0IamctRm8B9Bp+rvR59IA2NXDG1Ff/v1IddBGCwI0p+eQG5yMyEXvXX4PiHJTfnM8o2Jx+7PIx2TT8XlMEyo1a2O4x3z0BCDWFWmk8tsZi6I0QbRt/BZSuONHQVRjMG9SQQPUDiyDXjdKe7OBppjNOnnxFSFSswQUzvl0kaenHaWzptFlC3Zpz3Xb4gzDYGKUUArsf6TJnvach2lTPwgq7BhIBn/uDEhCCHzsNgzBebfl8rhADWygeAokq8QIDAQAB";

        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = "http://118.31.77.86:18181/payment/notify";

        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
//        config.encryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->";

        return config;
    }
}